function [X,Y,intLim] = ecogPrepareRegression(data,Y,intervalLengthSamp,intervalOffsetSamp)
%[W]=ecogPrepareRegression(ecog,Y,intervalLength,intervalOffset) 
%
% Purpose: Prepare X and Y in order to
%          Calculate the Multiple Input Multiple Output (MIMO) Wiener regression
% matrix W that does Y=X*W, i.e. predicts Y (the movement or stimulus feature)
% from X (the ecog data). Ridge regression is used to estimate W.
%
% Y is regressed with multiple shifted version of X. Therefore, W extends in
% time and establishes a certain temporal relation between X and Y that can
% be interpreted in terms of causality (see below). Heres X causal on Y means
% that effects in X have an influence on what happens later in time in Y.
%
% INPUT:
% data:     nChannels X nSamples
% Y:        The matrix of time series of external variables (measured movements,
%           or stimulus parameters). Variables change along the first
%           dimension and time increases along the second.
%           set Y empty in order to prepare Prediction data
% intervalLength:
%           The length of the intervall of the regression filter
%           (in units of samples). The number of estimated regression parameters
%           per external variable is intervalLengthInSamples*numberOfChannelsInX
% intervalOffset:
%           Time offset between the brain data and external variables:
%           Four cases can be distinguished:
%
%           intervalOffset<=-intervalLength:
% returns a regression marix W with purely
% causal effects of X on Y. If both absolute values are equal returns the
% W with the shortest lag with purely causal effects of X on Y. Decreasing
% the offset further (its a negative value!) produces X on Y causal Ws with
% longer lags
%
%           0>=intervalOffset>-intervalLength:
% returns a matrix W producing mixed X
% and Y on X causal effects.
%
%           intervalOffset>=1:
%produces the filter with the shortest lag with purely
% causal effects of Y on X. Further increasing the offset (its a positive
% value!) produces Y on X causal Ws with longer lags.
%
%           intervalOffset=0 and intervalLength=1
%           standard regression
%
% intLim:   interval limits for evaluation of Y and Yhat 
%
% see also: ecogTrainRegression; ecogPredictRegression
% CR 20120621: indexing revised
% try demonstration:
% [X,Y]=ecogPrepareRegression([1:10],1:10,2,-1);disp(Y');disp(X');

%% We make the lagged X matrix first

nChannels=size(data,1);
nShifts = intervalLengthSamp-1;

shiftIdx=(0:nShifts)+intervalOffsetSamp;

% the new number of samples is determined by the overlap of the Y and all
% shifted X columns and Y.
% CR: more samples can be included 6.6.20010
preXCut = max([0 min(shiftIdx)]); % good
postYCut = max([0 min(shiftIdx)+nShifts]); %good

postXCut = max([0 min(-shiftIdx)])+nShifts;
preYCut = max([0 max(-shiftIdx)]);

nSamplesNew = size(data,2)-postXCut-preXCut;

%pre allocat space. We throw away what is cut off by the lag
X=zeros(nSamplesNew,nChannels*length(shiftIdx));

%Make X
for k=1:length(shiftIdx)
    X(:,(k-1)*nChannels+1:k*nChannels)=data(:,preXCut+(k:k+nSamplesNew-1))';
end

intLim = [preYCut+1,size(data,2)-postYCut];

%%  now chop the Y matrix
% We may want to include ones for the offset
if ~isempty(Y),
    Y=Y(:,intLim(1):intLim(2))';
end